home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / print.cpp < prev    next >
C/C++ Source or Header  |  1994-10-10  |  8KB  |  315 lines

  1. #include <stdlib.h>
  2. #include "print.h"
  3. #include "simple.h"
  4. #include <bios.h>
  5. #include <dos.h>
  6. #include <conio.h>
  7.  
  8. /*
  9. #include <stdio.h>
  10. #include <io.h>
  11. #include <fcntl.h>
  12.  
  13.  
  14.  
  15. #include <string.h>
  16.  
  17. #include <assert.h>
  18.  
  19.  
  20. */
  21. // #include "filter.h"   // XXX
  22.  
  23. FILE * nonstdprn;
  24. int dcounter;
  25.  
  26. #define p_char(c) biosprint(_PRINTER_WRITE, c, 0)
  27.  
  28. void PrintManager::draw_string(char* str)
  29.     {
  30.     for(int i = 0; str[i]; i++)
  31.     p_char(str[i]);
  32.     }
  33. //////////////////////
  34. void PrintManager::init_printer(int size, int sh)
  35.     {
  36.     switch(printer_type)
  37.     {
  38.     case EPSON9:
  39.     case EPSON24:
  40.         p_char(27); p_char('$'); p_char(left % 256);
  41.             p_char(left / 256);        // set left margine
  42.         p_char(27); p_char('3'); p_char(sh);   // sh/216
  43.         int n1 = (size) % 256;
  44.         int n2 = (size) / 256;
  45.         p_char(27);                          // Set graphics line length n1 + 256 * n2
  46.         if(printer_type == EPSON9)
  47.         p_char(density == DD ? 'L' : 'Z');
  48.         else
  49.         {
  50.         p_char('*');
  51.         p_char(density == DD ? 33 : 39);
  52.         }
  53.         p_char(n1); p_char(n2);
  54.         break;
  55.     case LASERJET_II:
  56.         p_char(27); p_char(42); p_char(98);  // transfer
  57.         char str[10];
  58.         draw_string(itoa(size, str, 10));    p_char(87);
  59.         break;
  60.     }
  61.     }
  62. //////////////////////////
  63. void PrintManager::draw_pages(GrafBuffer* buf, char* work_name, int maxpage)
  64.     {
  65.     maxpage = maxpage / 8 * 8;
  66.     if(((buf->bound_size).Y = ((buf->bound_size).Y / parity) * parity) != 0
  67.     && maxpage >= buf->buf_dim.X)
  68.     draw_buffer(buf);
  69.     else
  70.     {
  71.     buf->bound_size.Y = buf->image->ymax + 1;
  72.     for(int page = 0; page < (buf->bound_size).X / maxpage + 1; page++)
  73.         {
  74.         int right = (maxpage * (page + 1) - 1 < buf->buf_dim.X - 1)
  75.             ? maxpage * (page + 1) - 1
  76.             : buf->buf_dim.X / 8 * 8 - 1;
  77.         print_part(rect(maxpage * page, 0, right,
  78.                  (buf->buf_dim).Y - 1), buf, work_name);
  79.         sound(200); delay(300); nosound();
  80.         if(getch() == 27)
  81.         break;
  82.         }
  83.     }
  84.     }
  85. //////////////////////////
  86. void PrintManager::print_part(rect src, GrafBuffer* buf, char* work_name)
  87.     {
  88.     buf->buffer_disk(src, work_name);
  89.  
  90.     loc res_buf = buf->buf_dim;
  91.     loc res_bound = buf->bound_size;
  92.     char* res_file_name = buf->file_name;
  93.  
  94.     buf->b_close();
  95.  
  96.     (buf->buf_dim).Y = src.height();
  97.     (buf->bound_size).X = (buf->buf_dim).X = src.width();
  98.     (buf->bound_size).Y =
  99.     (BOUND_SIZE / image_size(buf->bound_size.X, 1, 1, 4))
  100.         / parity * parity;
  101.     if(buf->bound_size.Y > buf->buf_dim.Y)
  102.     buf->bound_size.Y = buf->buf_dim.Y;
  103.  
  104.     buf->file_name = work_name;
  105.  
  106.     buf->b_open();
  107.  
  108.     draw_buffer(buf);
  109.     buf->b_close();
  110.  
  111.     buf->buf_dim = res_buf;
  112.     buf->bound_size = res_bound;
  113.     buf->file_name = res_file_name;
  114.  
  115.     buf->b_open();
  116.     }
  117. //////////////////////////
  118. void PrintManager::draw_buffer(GrafBuffer* buf) // prints buffer
  119.     {
  120.     int reserv_size = (buf->bound_size).Y;
  121.     int num_bounds = (buf->buf_dim).Y / (buf->bound_size).Y + 1;
  122.  
  123.     (buf->bound_size).Y =
  124.     (BOUND_SIZE / (image_size(buf->bound_size.X, 1, 1, 4)))
  125.         / parity * parity;
  126.  
  127.     if(printer_type == LASERJET_II)
  128.     {
  129.     p_char(27); p_char('E');
  130. //    p_char(27); p_char(42); p_char(114); p_char(0); p_char(65);  // graphics
  131.  
  132.     p_char(27); p_char(42); p_char(99);
  133.     char str[10];
  134.     draw_string(itoa((buf->buf_dim).X, str, 10)); p_char(65); // dimension
  135.  
  136.     p_char(27); p_char(42); p_char(99);
  137.     draw_string(itoa((buf->buf_dim).Y, str, 10)); p_char(86);
  138.  
  139. //    p_char(27); p_char(42); p_char('p'); draw_string(itoa(left, str, 10));
  140.  
  141.     p_char(27); p_char(42); p_char(116);
  142.     switch(density)
  143.         {
  144.         case DD: p_char('7'); p_char('5'); break;
  145.         case QD: p_char('3'); p_char('0'); p_char('0'); break;
  146.         case LJ_150: p_char('1'); p_char('5'); p_char('0'); break;
  147.         case LJ_100: p_char('1'); p_char('0'); p_char('0'); break;
  148.         }
  149.     p_char(82);     // resolution
  150.  
  151.     }
  152.     else
  153.     {
  154.     p_char(27); p_char('@');   // reset
  155.     p_char(27); p_char('U'); p_char(1);            // unidirectional print
  156. //    p_char(27); p_char(paper);                     // paper_out sensor on/off
  157.     }
  158.  
  159.     for(int i = 0; i < num_bounds && !kbhit(); i += buf->nplanes * buf->bitpx)
  160.     {
  161.     int bound_y = (buf->image)->ymax;
  162.     buf->get_BW(i);
  163.     buf->image->ymax = (buf->image->ymax > buf->buf_dim.Y)
  164.                  ? buf->buf_dim.Y - 1
  165.                  : (long)buf->image->ymax;
  166.  
  167.     draw_image(buf->image);
  168.     (buf->image)->ymax = bound_y;
  169.     }
  170.     if(printer_type == LASERJET_II)
  171.     {
  172.     p_char(13); p_char(10); p_char(12);
  173.     p_char(27); p_char(42); p_char(114); p_char(66);  // end graphics
  174.     p_char(27); p_char(69);  // reset
  175.     }
  176.     else
  177.     {
  178.     p_char(27);
  179.     p_char('@');   // reset
  180.     }
  181.     (buf->bound_size).Y = reserv_size;
  182.     }
  183. //////////////////////////
  184. void PrintManager::draw_image(imageP image)
  185.     {
  186.     uchar byte;
  187.     int prn_picture_width = (long)image->xmax * mx / dx + 1;
  188.     int prn_picture_height = (long)image->ymax * my / dy + 1;
  189.  
  190.     switch(printer_type)
  191.     {
  192.     case EPSON9:
  193.         int sh = 22;
  194.         bool sh1 = OFF;
  195.         for(int j = 0; j < prn_picture_height && !kbhit();    j += sh)
  196.         {
  197.         if(sh == 22)
  198.             { sh = 1; sh1 = !sh1; }
  199.         else if(sh1)
  200.             sh1 = !sh1;
  201.         else
  202.             sh = 22;
  203.         for(int p = 0; p < pass; p++)
  204.             {
  205.             init_printer(prn_picture_width, sh);
  206.             for(int i = 0; i < prn_picture_width; i++)
  207.             {
  208.             byte = 0;
  209.             for(int bit = 0; bit < 8; bit++)
  210.                 {
  211.                 uchar pix = image_get_pixel(image,
  212.                 loc((long)i * dx / mx, (long)(j + 3 * bit) * dy / my),
  213.                               1, 1);
  214.                 uchar lo = (byte << (bit + 1)) >> (bit + 1);
  215.                 uchar hi = (byte >> (8 - bit)) << (8 - bit);
  216.                 byte = hi | lo | (pix << (7 - bit));
  217.                 }
  218.             p_char(byte);
  219.             }
  220.             p_char(13);
  221.             }
  222.         p_char(10);
  223.         }
  224.         p_char(13);
  225.         break;
  226.     case EPSON24:
  227.         sh = 24;
  228.         for(j = 0; j < prn_picture_height && !kbhit(); j += sh)
  229.         {
  230.         for(int p = 0; p < pass; p++)
  231.             {
  232.             init_printer(prn_picture_width, sh);
  233.             for(int i = 0; i < prn_picture_width; i++)
  234.             {
  235.             for(int k = 0; k < 3; k++)
  236.                 {
  237.                 byte = 0;
  238.                 for(int bit = 0; bit < 8; bit++)
  239.                 {
  240.                 uchar pix = image_get_pixel(image,
  241.                       loc((long)i * dx / mx,
  242.                       (long)(j + k * 8 + bit) * dy / my), 1, 1);
  243.                 uchar lo = (byte << (bit + 1)) >> (bit + 1);
  244.                 uchar hi = (byte >> (8 - bit)) << (8 - bit);
  245.                 byte = hi | lo | (pix << (7 - bit));
  246.                 }
  247.                 p_char(byte);
  248.                 }
  249.             }
  250.             p_char(13);
  251.             }
  252.         p_char(10);
  253.         }
  254.         p_char(13);
  255.         break;
  256.     case LASERJET_II:
  257.         for(int i = 0; i < prn_picture_height && !kbhit(); i++)
  258.         {
  259.         init_printer(prn_picture_width / 8, 1);
  260.         for(j = 0; j < prn_picture_width / 8; j ++)
  261.             {
  262.             byte = 0;
  263.             for(int bit = 0; bit < 8; bit++)
  264.             {
  265.             uchar pix = image_get_pixel(image,
  266.                   loc((long)(j * 8 + bit) * dx / mx,
  267.                   (long)i * dy / my), 1, 1);
  268.             uchar lo = (byte << (bit + 1)) >> (bit + 1);
  269.             uchar hi = (byte >> (8 - bit)) << (8 - bit);
  270.             byte = hi | lo | (pix << (7 - bit));
  271.             }
  272.             p_char(byte);
  273.             }
  274.         }
  275.         break;
  276.     }
  277.     }
  278. //////////////////////////////
  279. /*
  280. void main()
  281.     {
  282.     int gdriver = DETECT;
  283.     int gmode;
  284.     initgraph(&gdriver, &gmode, "");
  285.  
  286.     PrintManager p(EPSON9, DD, 2, rect(1, 1, 1, 1), 0, PAPER_ON, 6);
  287.     GrafBuffer* g = new GrafBuffer(loc(750, 590), "work.buf",
  288.                    rect(0, 0, 500, getmaxy() - 1));
  289.  
  290.     g->b_open();
  291.     g->clear();
  292.     pcx_file_buffer(g, loc(0, 0), "kh_draw.pcx");  // This file shows the
  293.     g->buffer_screen();                            // KNOW-HOW.DRAW PCX
  294.                                                    // editor (source codes
  295. //    p.draw_buffer(g);                              // are available).
  296. //    p.draw_pages(g, "work1.buf", 320);
  297.  
  298.     pcx_file_buffer(g, loc(0, 0), "inbi.pcx");    // Black and White image
  299.     g->buffer_screen();
  300.  
  301.     p.draw_buffer(g);
  302.  
  303.     p.set_comp(1, 2, 2, 1);
  304.     p.draw_buffer(g);
  305.  
  306.     p.set_comp(1, 1, 2, 2);
  307.     p.draw_buffer(g);
  308.  
  309.     p.print_part(rect(64, 50, 239, 200), g, "work1.buf");
  310.  
  311.     g->b_close();
  312.     delete g;
  313.     closegraph();
  314.     }
  315. */